package com.example.flutter_app_echarts;

import android.annotation.TargetApi;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
import java.util.UUID;
import java.io.BufferedReader;

public class BluetoothUtils {
    public BluetoothAdapter mBluetooth;
    public BluetoothSocket mSocket;
    public BluetoothServerSocket mServerSocket;
    public BlueToothConnectReceiver blueToothConnectReceiver;
    public UUID uuid;

    @TargetApi(Build.VERSION_CODES.ECLAIR)
    public BluetoothUtils() {
        this.mBluetooth = BluetoothAdapter.getDefaultAdapter();
        this.uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
    }

    @TargetApi(Build.VERSION_CODES.ECLAIR)
    public void connect() {
        System.out.println(this.uuid);
        System.out.println("println输入日志信息");
    }

    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    public String connectState(String address) throws IOException {
//        String deviceId = "20:20:03:27:31:74";
        String deviceId = "38:BA:F8:89:00:B1";
//        String deviceId = "E7:0C:2D:E1:F7:43";
        System.out.println(address);
        BluetoothDevice device = this.mBluetooth.getRemoteDevice(address);
        mSocket = device.createRfcommSocketToServiceRecord(this.uuid);
        Timer nTimer = new Timer();
        nTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("----是否连接成功-----");
                System.out.println(mSocket.isConnected());
            }
        }, 2000);

        if (mSocket.isConnected()) {
            System.out.println("已连接成功");
            return "200";
        } else {
            System.out.println("尚未连接成功");
            return "404";
        }

    }

    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    public BufferedReader inputStreamNotify(MainActivity mainActivity) throws IOException {
        System.out.println(this.mSocket);
        System.out.println(this.mSocket.isConnected());
        if (this.mSocket != null && this.mSocket.isConnected()) {
            InputStream inputStream = this.mSocket.getInputStream();
            InputStreamReader StreamReader = new InputStreamReader(inputStream);
            BufferedReader CodeReader = new BufferedReader(StreamReader);
            return CodeReader;
        } else {
            return null;
        }
    }

    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    public Boolean isEnabled() {
        if (this.mBluetooth != null) {
            Set devices = this.mBluetooth.getBondedDevices();
            Iterator iterator = devices.iterator();
            while (iterator.hasNext()) {
                BluetoothDevice device = (BluetoothDevice) iterator.next();
                String device_name = device.getName();
                String device_address = device.getAddress();
                System.out.println(device_name);
                System.out.println(device_address);
            }
            System.out.println("this.mBluetooth.isEnabled()");
            System.out.println(this.mBluetooth.isEnabled());
            return this.mBluetooth.isEnabled();
        } else {
            return false;
        }
    }

    public void registerBlueToothConnectReceiver(MainActivity activity) {
        blueToothConnectReceiver = new BlueToothConnectReceiver();
        IntentFilter filter_connect = new IntentFilter();
        filter_connect.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        filter_connect.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
        filter_connect.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
        activity.registerReceiver(blueToothConnectReceiver, filter_connect);
        IntentFilter filter_disconnect = new IntentFilter();
        filter_disconnect.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        filter_disconnect.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
        filter_disconnect.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
        activity.registerReceiver(blueToothConnectReceiver, filter_disconnect);
        System.out.println("===============BlueToothConnectReceiver================");
        blueToothConnectReceiver.setOnBleConnectListener(new BlueToothConnectReceiver.OnBleConnectListener() {
            @Override
            public void onConnect(BluetoothDevice device) {

            }

            @Override
            public void onDisConnect(BluetoothDevice device) {

            }
        });
    }

    public void unregisterBlueToothConnectReceiver(Activity activity) {
        activity.unregisterReceiver(blueToothConnectReceiver);
    }
}
